package org.xinjiang.data.migration.common.enums;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author wenjie
 * @date 2023/3/12
 */
public interface BaseEnum extends Serializable {
    /**
     * 根据code 获取 枚举
     *
     * @param c    枚举class
     * @param code code
     * @param <T>  枚举
     * @return 枚举
     */
    static <T extends Enum> T of(Class<T> c, String code) {
        if (code == null) {
            return null;
        }
        for (T e : c.getEnumConstants()) {
            if (e instanceof BaseEnum) {
                BaseEnum o = (BaseEnum) e;
                if (Objects.equals(o.getCode(), code)) {
                    return e;
                }
            }
        }
        return null;
    }

    /**
     * 获取code
     *
     * @return code
     */
    String getCode();

    /**
     * 获取描述
     *
     * @return 描述
     */
    String getDescribe();

}
